package com.project.petLossSystem.service.impl;

import com.project.petLossSystem.common.ServiceResultEnum;
import com.project.petLossSystem.mapper.IndexCarouselMapper;
import com.project.petLossSystem.po.IndexCarousel;
import com.project.petLossSystem.service.IndexCarouselService;
import com.project.petLossSystem.util.MyBeanUtil;
import com.project.petLossSystem.util.PageQueryUtil;
import com.project.petLossSystem.util.PageResult;
import com.project.petLossSystem.vo.IndexCarouselVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class IndexCarouselServiceImpl implements IndexCarouselService {
    @Autowired
    private IndexCarouselMapper indexCarouselMapper;

    @Override
    public boolean insertCarouse(String carouselUrl, String redirectUrl) {
        // carouselUrl一定存在，参考controller
        if (redirectUrl.isEmpty()) {
            redirectUrl = "#";
        }
        return indexCarouselMapper.insertCarousel(carouselUrl, redirectUrl) > 0;
    }

    @Override
    public PageResult getCarouselPage(PageQueryUtil pageQueryUtil) {
        List<IndexCarousel> carousels = indexCarouselMapper.carouselList(pageQueryUtil);
        int total = indexCarouselMapper.carouselCount(pageQueryUtil);
        return new PageResult(total, pageQueryUtil.getRows(), pageQueryUtil.getPage(), carousels);
    }

    @Override
    public String updateCarousel(IndexCarousel carousel) {
        IndexCarousel tempCarousel = indexCarouselMapper.getCarouselById(carousel.getCarouselId());
        if (tempCarousel == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        // 修改只有跳转链接和时间需要改
        carousel.setUpdateTime(new Date());
        if (indexCarouselMapper.updateCarousel(carousel) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        } else {
            return ServiceResultEnum.DB_ERROR.getResult();
        }
    }

    @Override
    public boolean deleteCarousels(Integer[] carouselIds) {
        return indexCarouselMapper.simpleDeleteCarousels(carouselIds) > 0;
    }

    @Override
    public List<IndexCarouselVO> getCarouselsForIndex() {
        List<IndexCarousel> carousels = indexCarouselMapper.getCarousels();
        if(CollectionUtils.isEmpty(carousels)){
            return null;
        }
        List<IndexCarouselVO> carouselVOs = MyBeanUtil.copyList(carousels, IndexCarouselVO.class);
        return carouselVOs;
    }
}
